package com.bys.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class Knife4jConfig {
    private static List<Parameter> getTokenParam() {
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder tokenParam = new ParameterBuilder();
        Parameter token = tokenParam.name("token")
                .defaultValue("")
                .description(" 用户的 token")
                .modelRef(new ModelRef("string"))
                .parameterType(ParameterType.HEADER)
                .required(false)
                .build();

        pars.add(token);
        return pars;
    }
    @Bean
    public Docket systemApiconfig(){
        return new Docket(DocumentationType.SWAGGER_2)
               .groupName("systemApiInfo")
               .apiInfo(oaApiInfo())
               .select()
               .apis(RequestHandlerSelectors.basePackage("com.bys.system"))
               .paths(PathSelectors.regex("/system/.*"))
               .build()
               .globalOperationParameters(getTokenParam());

    }
    @Bean
    public Docket oaApiconfig(){
       return  new Docket(DocumentationType.SWAGGER_2)
                .groupName("oaApiInfo")
                .apiInfo(oaApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.bys.oa"))
                .paths(PathSelectors.regex("/oa/.*"))
                .build()
                .globalOperationParameters(getTokenParam());
        
    }

    private ApiInfo oaApiInfo() {
        return new ApiInfoBuilder().title(" bys_crm -后台管理系统 ")
                .description(" 描述后台管理系统的 接口")
                .version("1.0")
                .contact(new Contact("bys","http://bys.com","13403504277@163.com"))
                .build();
    }

}
